package com.atguigu.process.service.impl;
//2 第一步分页查询返回分页数据，从分页数据获取列表List集合

//3 遍历List集合，得到每个对象的审批类型id

//4 根据审批米刑d查冶燕血对成名称

//5 完成最终

import cn.hutool.core.util.StrUtil;
import com.atguigu.model.base.BaseEntity;
import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.model.process.ProcessType;
import com.atguigu.process.mapper.OaProcessTemplateMapper;
import com.atguigu.process.mapper.OaProcessTypeMapper;
import com.atguigu.process.service.OaProcessService;
import com.atguigu.process.service.OaProcessTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批模板 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-10-25
 */
@Service
public class OaProcessTemplateServiceImpl extends ServiceImpl<OaProcessTemplateMapper, ProcessTemplate> implements OaProcessTemplateService {

    @Autowired
    private OaProcessTemplateMapper processTemplateMapper;

    @Autowired
    private OaProcessTypeMapper processTypeMapper;

    @Autowired
    private OaProcessService processService;

    // 分页查询审批模板，把审批类型对应名称查询
    @Override
    public IPage<ProcessTemplate> selectPageProcessTempate(Page<ProcessTemplate> pageParam) {
        //1 调用mapper的方法实现分页查询
        Page<ProcessTemplate> processTemplatePage =
                processTemplateMapper.selectPage(pageParam, null);
        //2 查询到的结果集
        List<ProcessTemplate> processTemplateList = processTemplatePage.getRecords();

        //3 遍历List集合，得到每个对象的审批类型id
        for (ProcessTemplate processTemplate : processTemplateList) {
            //4 根据审批类型 Id，查询获取对应名称
            if (!ObjectUtils.isEmpty(processTemplate)) {
                Long processTypeId = processTemplate.getProcessTypeId();
                LambdaQueryWrapper<ProcessType> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(BaseEntity::getId, processTypeId);
                ProcessType processType = processTypeMapper.selectOne(wrapper);
                //4.1 储存在结果集
                processTemplate.setProcessTypeName(processType.getName());
            }
        }
        //5 完成最终
        return processTemplatePage;
    }

    // 修改状态值, 发布
    @Override
    public void publish(Long id) {
        // 根据ID查询到, 修改状态码为 1
        ProcessTemplate processTemplate = processTemplateMapper.selectById(id);
        processTemplate.setStatus(1);
        processTemplateMapper.updateById(processTemplate);

        // 流程部署定义
        // 优先发布在线流程设计
        if (!StrUtil.isEmpty(processTemplate.getProcessDefinitionPath())) {
            processService.deployByZip(processTemplate.getProcessDefinitionPath());
        }
    }
}




























